package com.aiyige.page.my.customer.dialog;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.aiyige.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class CustomerSexBottomDialog extends DialogFragment {
    public static final String FEMALE = "f";
    public static final String MALE = "m";
    public static final String UNKNOWN = "x";
    @BindView(R.id.maleBtn)
    TextView maleBtn;
    @BindView(R.id.femaleBtn)
    TextView femaleBtn;

    Unbinder unbinder;
    private String sex = UNKNOWN;
    Listener listener = null;

    public CustomerSexBottomDialog() {
    }

    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public static CustomerSexBottomDialog newInstance() {
        CustomerSexBottomDialog fragment = new CustomerSexBottomDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.BottomDialogTheme);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.customer_sex_bottom_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        updateView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void updateView() {
        maleBtn.setSelected(sex.equals(MALE));
        femaleBtn.setSelected(sex.equals(FEMALE));
    }

    @OnClick({R.id.maleBtn, R.id.femaleBtn,R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.maleBtn:
                sex = MALE;
                break;
            case R.id.femaleBtn:
                sex = FEMALE;
                break;
            case R.id.cancelBtn:
                dismiss();
                return;
        }
        updateView();
        if (listener != null) {
            listener.onClick(sex);
        }
        dismiss();
    }

    public interface Listener {
        void onClick(String sex);
    }
}
